/**
 * Created by charsen on 2017/8/10.
 */
var notneeded = ['运费', '邮费', '拖鞋', '凉鞋', '背包', '清洗剂', '擦鞋', '清洁', '工具', '毛刷', '轻便', '纸巾', '沙滩', '裤', '喷雾'];

window.onload = function () {
    // 如果需要打码
    if (document.querySelector(".sufei-dialog.sufei-dialog-kissy") && document.querySelector(".sufei-dialog.sufei-dialog-kissy").style.display == 'block') {
        return;
    }

    setTimeout(function () {
        var links = [];

        for (var i = 0, items = document.querySelectorAll('.shop-hesper-bd.grid .item3line1 dl dd.detail a'); i < items.length; i++) {
            var title = items[i].innerText;
            var valid = true;

            for (var j = 0; j < notneeded.length; j++) {
                if (title.indexOf(notneeded[j]) != -1) {
                    valid = false;
                }
            }

            if (valid) {
                links.push(items[i]);
            }
        }

        if (links.length) {
            links.shift().click();
        }

        // 监听爬取结果，按照队列一个一个爬取
        chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
            if (request.name == "scrapy next") {
                if (links.length) {
                    setTimeout(function () {
                        links.shift().click();
                    }, Math.random() * 2000);
                } else {
                    // 查看是否有下一页
                    if (document.querySelector('.J_SearchAsync.next')) {
                        document.querySelector('.J_SearchAsync.next').click()
                    }
                }
            }
        });
    }, 1000);
}

